<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% @body_classes << 'assessing' if @body_classes %>
<% content_for :page_title do %><%= join_title @quiz.readable_type, @quiz.quiz_title %><% end %>

<% content_for :meta_tags do %>
  <% if params[:persist_headless] %>
    <meta name="viewport" id="vp" content="initial-scale=1.0,user-scalable=yes,width=device-width">
    <% js_env :MOBILE_UI => true %>
  <% end %>
<% end %>

<% content_for :right_side do %>
  <%= render "take_quiz_right_side" %>
<% end %>
<% css_bundle :quizzes, :tinymce %>

<h3 class="loading"><%= t "Loading..." %></h3>
<div class="loaded" style="display:none">
  <header class="quiz-header">
    <h1><%= @quiz.quiz_title || @quiz.readable_type %></h1>
    <% if @submission.preview? %>
      <p>
        <div class="alert">
          {{> icons/warning}}
          <% if @quiz.published? %>
            <%= t 'This is a preview of the published version of the quiz' %>
          <% else %>
            <%= t 'This is a preview of the draft version of the quiz' %>
          <% end %>
        </div>
      </p>
    <% end %>
    <%= before_label(:started, "Started") %>
    <%= datetime_string(@submission.started_at) %>
    <h2>{{#t}}Quiz Instructions{{/t}}</h2>
    <div id="quiz-instructions" class="user_content">
      <%= user_content(@quiz.description) %>
    </div>

    <% if @quiz.time_limit %>
      <div class="screenreader-only" role="note">
        <%= t 'audible_quiz_timer_shortcut_description', <<-DESCRIPTION
          Note: this is a timed quiz. You may check the remaining time you have
          at any point while taking the quiz by pressing the keyboard combination
          SHIFT, ALT, and T...

          Again: SHIFT, ALT, and T...
          DESCRIPTION
        %>
      </div>
    <% end %>
  </header>

  <% js_bundle :take_quiz %>

  <%= form_tag @quiz_presenter.form_action(session, @current_user), :id => "submit_quiz_form", :class => @quiz_presenter.form_class, :method => "post" do %>

  <div id="questions" class="assessing">
    <input type="hidden" name="attempt" value="<%= @submission.attempt %>"/>
    <% if @submission.validation_token.present? %>
      <input type="hidden" name="validation_token" value="<%= @submission.validation_token %>"/>
    <% end %>
    <% if @submission.preview? %>
      <input type="hidden" name="preview" value="1"/>
    <% end %>
    <div style="display: none;" id="quiz_urls">
      <a href="<%= context_url(@context, :backup_context_quiz_quiz_submissions_url, @quiz.id) %>?user_id=<%= @current_user && @current_user.id %><%= "&preview=1" if @submission.preview? %>" class="backup_quiz_submission_url">&nbsp;</a>
      <span class="started_at"><%= @submission.started_at.iso8601 %></span>
      <span class="now"><%= ActiveSupport::TimeWithZone.new(Time.now.utc, Time.zone).iso8601 %></span>
      <span class="end_at"><%= @submission.end_at && @submission.end_at.iso8601 %></span>
      <span class="due_at"><%= @quiz.due_at && @quiz.due_at.iso8601 %></span>
      <span class="time_limit"><%= @quiz.time_limit || nbsp %></span>
      <span class="time_left"><%= @submission.time_left %></span>
    </div>

    {{> views/display_question}}

    <div class="button-container clearfix">
      <% if @quiz_presenter.one_question_at_a_time? %>
        <% if @quiz_presenter.previous_question_viewable? %>
          <% previous_question_button_title = t('buttons.previous', "Previous") %>
          <button type="submit" class="Button submit_button previous-question" data-action="<%= @quiz_presenter.previous_question_form_action(session, @current_user) %>" disabled>
            {{> icons/mini-arrow-left}}
            <%= previous_question_button_title %>
          </button>
        <% end %>

        <% if @quiz_presenter.next_question %>
          <% next_question_button_title = t('buttons.next', "Next") %>
          <%= hidden_field_tag :last_question_id, @quiz_presenter.current_question[:id] %>

          <button type="submit" class="Button submit_button  next-question" data-action="<%= @quiz_presenter.next_question_form_action(session, @current_user) %>" disabled>
            <%= next_question_button_title %>
            {{> icons/mini-arrow-right}}
          </button>
        <% end %>
      <% end %>
    </div>
  </div>

  <div style="font-size: 1.2em; display: none;" id="times_up_dialog">
    <%= before_label(:times_up, "Time's Up!  Submitting results in") %>
    <div style="text-align: center; font-size: 1.2em; margin: 10px;" class="countdown_seconds"></div>
    <div class="button-container">
      <button type="submit" class="submit_quiz_button btn quiz_submit">
        {{#t}}Ok, fine{{/t}}
      </button>
    </div>
  </div>

  <div class=form-actions>
    <span id="last_saved_indicator">{{#t}}Not saved{{/t}}</span>
    <button type="submit" class="btn submit_button quiz_submit btn-secondary" id="submit_quiz_button" data-action="<%= @quiz_presenter.submit_form_action(session, @current_user) %>">
      {{#t}}Submit Quiz{{/t}}
    </button>
  </div>

  <% end %>

  <form id="deauthorized_dialog"
    class="hide"
    title="<%= t :login_required, "Login Required" %>"
    action="<%= course_quiz_path(@context, @quiz) %>"
    method="get">
    <div class="text-center">
      <%= t :logged_out_of_canvas,
            "*You have been logged out of canvas.* To continue please log in",
            :wrapper => '<strong>\1</strong><br />' %>
    </div>
  </form>
</div>
